package ppa.application.model;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.Collection;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ppa.application.controller.LoadController;
import ppa.application.dom.XMLParser;

public class GameWindow extends JFrame {

	/**
	 * @test cm-t: test de version
	 */
	private static final long serialVersionUID = 1L;

	private JButton btLoad;
	private JButton btValid;
	private JButton btSave;

	private JTextArea taGame;
	private JTextField tfInput;

	public GameWindow() {
		super();

		buildWindow();
	}

	public JButton getBtLoad() {
		return btLoad;
	}

	public JButton getBtValid() {
		return btValid;
	}

	public JButton getBtSave() {
		return btSave;
	}

	public JTextArea getTaGame() {
		return taGame;
	}

	public JTextField getTfInput() {
		return tfInput;
	}

	private void buildWindow() {
		setTitle("XML RPG Game Loader");
		setSize(450, 550);
		setLocationRelativeTo(null);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(buildContentPane());
	}

	private JPanel buildContentPane() {
		JPanel panel = new JPanel();
		BorderLayout bl = new BorderLayout(5, 10);
		panel.setLayout(bl);
		panel.setBackground(Color.BLACK);

		taGame = new JTextArea("test");
		taGame.setEditable(false);
		panel.add(taGame, BorderLayout.CENTER);

		/*
		 * Panel North
		 */
		JPanel pNorth = new JPanel();
		pNorth.setLayout(new FlowLayout());

		btLoad = new JButton("Load game");
		btSave = new JButton("Save Game");

		LoadController calcController = new LoadController(this);
		btLoad.addActionListener(calcController);

		pNorth.add(btSave);
		pNorth.add(btLoad);

		/*
		 * Panel South
		 */
		JPanel pSouth = new JPanel();
		pSouth.setLayout(new FlowLayout());

		tfInput = new JTextField();
		tfInput.setColumns(20);

		btValid = new JButton("Go !");

		pSouth.add(tfInput);
		pSouth.add(btValid);

		/*
		 * Adding Panels to Main panel
		 */

		panel.add(pNorth, BorderLayout.NORTH);
		panel.add(pSouth, BorderLayout.SOUTH);

		return panel;
	}

	public void loadGames() {
//		XMLParser parser = new XMLParser("xml/candidate.xml");
//		Map<Integer, String> map = parser.getGames();
//		
//		Collection<String> strList = map.values();
		
//		String[] str = strList.toArray(new String[0]); //Collection to array 
				
		JOptionPane opt = new JOptionPane();
		String nom = (String) opt.showInputDialog(null,
				"Please choose a game !", "Load Game !",
				JOptionPane.QUESTION_MESSAGE, null, str, str[0]);
		
		taGame.setText("You selected " + nom);
	}
}
